package com.swn.cm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swn.cm.service.StudentsService;
import com.swn.common.util.DateUtil;
import com.swn.common.util.JSONUtil;
import com.swn.common.util.PageListData;

/**
 * 
* @ClassName: DongStudentController
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Administrator
* @date 2018年8月4日
*
 */
@Controller
@RequestMapping("/dongStuInfo")
public class DongStudentController {
	
	
	@Autowired
	
	private StudentsService studentsService;
	
	/**
	 * 查询学生信息列表
	 * @Author:dongliang
	 * @param start
	 * @param length
	 * @param signupForm
	 * @return
	 */
	@RequestMapping("/queryStudentsList")
	@ResponseBody
	public Map<String, Object> queryStudentsList(Integer start,Integer length,String signupForm){
		Map<String, Object> map = new HashMap<String, Object>();
		PageListData pageListData = new PageListData();
		try {
			pageListData.setStart(start);
			pageListData.setLength(length);
			@SuppressWarnings("unchecked")
			Map<String, Object> param = JSONUtil.getMapFromJsonString(signupForm);
			List<Map<String, Object>> studentsList = studentsService.queryStudentsList(pageListData,param);
			int total = pageListData.getCount();
			System.out.println("---> "+total);
			map.put("recordsTotal", total);  // 总记录数
			map.put("recordsFiltered", total); // 过滤后的总记录数
			map.put("data", studentsList);   
		} catch (Exception e) {
			logger.info("查询保存了");
			System.out.println(e.getMessage());
		}
		return map;
		
	}
	
	/**
	 * 添加学生信息
	 * @param addJsonStr
	 * @return String
	 * @Author dongliang
	 * @Date 2018年8月4日 
	 * @Note <这里是方法的备注>
	 * @company 济南舒卫能生物技术有限公司
	 */
	@RequestMapping("/addStudent")
	@ResponseBody
	public String addStudent(String signupForm){
		
		@SuppressWarnings("unchecked")
		Map<String, Object> paraMap = JSONUtil.getMapFromJsonString(signupForm);
		paraMap.put("createtime", DateUtil.formatDateTime(DateUtil.getCurrentlyDate(), "yyyy-MM-dd"));
		int flag = studentsService.addStudents(paraMap);
		if(flag==0){
			return "error";
		}
		return "success";
	}
	
	
	/**
	 * 
	* @Title: updateStudent
	* @Description: 根据ID 编号修改学生信息
	* @author Administrator
	* @param @param signupForm
	* @param @param id
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping("/updateStudent")
	@ResponseBody
	public String updateStudent(String signupForm,long id){
		@SuppressWarnings("unchecked")
		Map<String,Object> paramMap = JSONUtil.getMapFromJsonString(signupForm);
		paramMap.put("id", id);
		if(studentsService.updateStudent(paramMap)<=0){
			return "error";
		}
		return "success";
	}
	
	/**
	 * 根据学生的编号查询 学生信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryStudentById")
	@ResponseBody
	public Map<String, Object> queryStudentById(Long id) throws Exception{
		Map<String, Object> teacherInfo = studentsService.queryStudentById(id);
		return teacherInfo;
	}
	
    /**
     * 
     * 删除学生信息
     * @param id
     * @return
     */
   
	@RequestMapping("/deleteStudent")
	@ResponseBody
	public String deleteStudent(long id){
		if(studentsService.deleteStudent(id)<=0){
			return "error";
		}
		return "success";
	}
	
	/**
	 * 查询班级信息
	 * @return
	 */
	@RequestMapping(value= "/queryClassList",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> queryClassList (){
		
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			List<Map<String,Object>> list = studentsService.queryClassList();
			map.put("data", list);
		}catch(Exception e){
			logger.error(e);
		}
		return map;
	}
	Logger logger = Logger.getLogger(DongStudentController.class);
	
	
	
}
